<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<a href="javascript:void(0);" onclick="$('#win_create_quotation').dialog('close');" class="window-close"></a>
 <dl class="pc_m" style="width: 500px;">
 	<input type="hidden" id="order_days" value="${days}" />
 	<div class="form" style="width: 500px;" server="${server.id}">
 		<p>
		 	<span>
		 	寄养日期:
		 	<fmt:formatDate value="${order.orderStartDate}" type="date" pattern="yyyy/MM/dd"/>
		 	 - 
		 	<fmt:formatDate value="${order.orderEndDate}" type="date" pattern="yyyy/MM/dd"/>
		 	(共计${days}天)
		 	</span>
	 	</p>
 	</div>
 	
 	<c:forEach items="${serverList}" var="server">
    <div class="form" style="width: 500px;" server="${server.id}" valueType="${server.valueType}">
    	<p style="width: 500px;">
    		<label>
    			<span>${server.serverName}</span> （按${server.valueType==0?"天":"次"}计费）
    		</label>
    	</p>
        <c:forEach items="${server.petServerValuationList}" var="valuation">
        <div style="display: block;float: none;width: 400px;height:40px; list-style:initial;margin: 0px;">
        	<input type="hidden" valueType="${server.valueType}" valuation="${valuation.id}" value="<fmt:formatNumber value="${valuation.valueVal}" type="currency" pattern=".00"/>"/>
        	<p class="fl" style="margin-left: 30px;">
        		${valuation.valueName}
        		单价:<span style="color: red;font-size: 12px;">￥<fmt:formatNumber value="${valuation.valueVal}" type="currency" pattern=".00"/></span>
        	</p>
        	<c:if test="${server.valueType != 0}">
        	<div class="input_num_panel">
        		<span class="input_num_left" title="-1" onclick="setValuationNum(this, -1)"></span>
        		<input class="input_num" valuationNum="${valuation.id}" type="text" value="0"/>
        		<span class="input_num_right" title="+1" onclick="setValuationNum(this, 1)"></span>
        		<span class="input_num_clear" title="清空" onclick="setValuationNum(this, 0)"></span>
        	</div>
        	</c:if>
        </div>
        </c:forEach>
    </div>
    </c:forEach>
    <div class="form" style="width: 500px;">
    	<p style="width: 500px;">
    		订单小计:<span id="sum_val">￥0.00</span>
    	</p>
    	<p style="width: 500px;">
    		价格调整:<span id="sum_val">￥<input class="input_price" id="input_tiaozheng" value="0"/></span>(可以为负数)
    	</p>
    	<p style="width: 500px;">
    		订单合计:<span id="sum_val_all">￥0.00</span>
    	</p>
    </div>
    <div class="btn" style="width: 500px;"><a href="javascript:submitQuotation()">提交</a></div>
</dl>


<script type="text/javascript">
var submitQuotation = function(){
	var submitData = {"order.id":"${param['id']}"};
	var subIndex = 0;
	$("input[valuation]").each(function(i, valuation){
		var valId = $(this).attr("valuation");
		var valNum = $("input[valuationNum='"+valId+"']").val();
		if($(this).is("[valueType='1']") && valNum > 0) {
			submitData["serverList[" + subIndex + "].valId"] = valId;
			submitData["serverList[" + subIndex + "].serverMeasure"] = $("input[valuationNum='" + valId + "']").val();
			subIndex++;
		} else if($(this).is("[valueType='0']")){
			submitData["serverList[" + subIndex + "].valId"] = valId;
			subIndex++;
		}
	});
	submitData["discount"] = $("#input_tiaozheng").val();
	$.post("${ctx}/order/saveQuotation.do", submitData, function(res){
		if(res.code == 1) {
			//msg.msg("报价单创建成功。");
			//$('#win_create_quotation').dialog('close');
			tools.href();
		} else {
			msg.error(res.msg);
		}
	}, "json");
}

function setValuationNum(obj, num) {
	$(obj).parent().children("input:text").each(function(){
		if(num == 0) {
			$(this).val(0);
			sumValuationAll();
		} else if($.isNumeric($(this).val())) {
			var oldValuationNum = parseInt($(this).val());
			if(num > 0 || (num < 0 && oldValuationNum > 0)) {
				$(this).val(oldValuationNum + num);
				sumValuationAll();
			}
		}
	});
}
function sumValuationAll() {
	var allSum = 0;
	$("div[server]").each(function(i, svr){
		var serSum = 0;
		if($(this).attr("valueType") == 0) {
			var val = $(this).find(" input[valuation]").val();
			var valNum = parseInt($("#order_days").val());
			serSum = (valNum * parseFloat(val));
		} else {
			$(this).find(" input:text[valuationNum]").each(function(i, val){
				var valNum = parseInt($(this).val());
				var val = $(svr).find(" input[valuation='" + $(this).attr("valuationNum") + "']").val();
				serSum += (valNum * parseFloat(val));
			});
		}
		allSum += serSum;
	});
	$("#sum_val").html("￥" + allSum.toFixed(2));
	var tiaozheng=0;
	if($.isNumeric($("#input_tiaozheng").val())) {
		tiaozheng = parseFloat($("#input_tiaozheng").val());
	}
	$("#sum_val_all").html("￥" + (allSum + tiaozheng).toFixed(2));
}

$(function(){
	//初始计算按天计价服务
	sumValuationAll();
	
	$("input:text[valuationNum]").blur(function(){
		sumValuationAll();
	});
	$("#input_tiaozheng").blur(function(){
		sumValuationAll();
	});
});
</script>
</body>
</html>